//
//  OilDropManageViewController.m
//  阿帕
//
//  Created by mac on 2020/5/26.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "OilDropManageViewController.h"
#import "OilPaymentViewController.h"
#import "OilDropRecordViewController.h"
#import "MyCenterCell.h"

@interface OilDropManageViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray     *dataArray;
@property (nonatomic, strong) UIView      *tableHeaderView;

@end

@implementation OilDropManageViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    [self setupNav];
    
    self.view.backgroundColor = BackgroundColor;
    
    self.navigationItem.title = @"油点管理";
    
    self.tableView.tableHeaderView = self.tableHeaderView;
    
    NSDictionary *userInfo = [UserDefaultUtil valueForKey:Login_Branch];
    
    UILabel *oilYue = [self.view viewWithTag:1000];
    oilYue.text = CJStringWithFormat(@"%.2f", [userInfo[@"oilCard"] floatValue]);
    
}
- (void)setupNav {
    [DCSpeedy changeNavControllerGradientView:self andBgColor:[UIImage gradientImageWithBounds:self.navigationController.view.bounds andColors:@[[UIColor cy_colorWithHexString:@"#1867E0"], [UIColor cy_colorWithHexString:@"#2171EA"]] andGradientType:GradientDirectionLeftToRight] andTitleColor:[UIColor whiteColor]];
    
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed:@"gcy_img_nav_lArrowTwo"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 17, 32);
    [button addTarget:self action:@selector(backButtonTapClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.titleLabel.text = [self.dataArray objectAtIndex:indexPath.row][@"title"];
    cell.iconView.image = image([self.dataArray objectAtIndex:indexPath.row][@"icon"]);
    if (indexPath.row == self.dataArray.count - 1) {
        cell.hLine.hidden = YES;
    } else {
        cell.hLine.hidden = NO;
    }
    cell.titleLabel.textColor = Dark_TextColor;
    cell.titleLabel.font = font(15);
    cell.isBindLabel.hidden = YES;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        OilPaymentViewController *opvc = [[OilPaymentViewController alloc] init];
        [self.navigationController pushViewController:opvc animated:YES];
        
    } else if (indexPath.row == 1) {
        
        OilDropRecordViewController *odrvc = [[OilDropRecordViewController alloc] init];
        [self.navigationController pushViewController:odrvc animated:YES];
        
    }
}

- (void)backButtonTapClick {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - lazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - TabBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackgroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyCenterCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSArray alloc] initWithObjects:
//                      @{@"title": @"油点余额", @"icon": @"gcy_icon_oil_yue"},
                      @{@"title": @"加油付款", @"icon": @"gcy_icon_oil_fukuan"},
                      @{@"title": @"油卡变动记录", @"icon": @"gcy_icon_oil_jilu"},nil];
    }
    return _dataArray;
}

- (UIView *)tableHeaderView {
    if (!_tableHeaderView) {
        _tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 185)];
        
        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 185)];
        bgImageView.image = [UIImage gradientImageWithBounds:bgImageView.bounds andColors:@[[UIColor cy_colorWithHexString:@"#1867E0"], [UIColor cy_colorWithHexString:@"#2171EA"]] andGradientType:GradientDirectionLeftToRight];
        [_tableHeaderView addSubview:bgImageView];
        
        UIImageView *centerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 35, ScreenW, 150)];
        centerImageView.image = [UIImage imageNamed:@"gcy_img_oil_yuebg"];
        centerImageView.contentMode = UIViewContentModeScaleAspectFit;
        [bgImageView addSubview:centerImageView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 10, 100, 40)];
        titleLabel.text = @"油点余额";
        titleLabel.font = font(17);
        titleLabel.textColor = [UIColor whiteColor];
        [bgImageView addSubview:titleLabel];
        
        UILabel *oilLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 60, 300, 50)];
        oilLabel.text = @"0";
        oilLabel.tag = 1000;
        oilLabel.font = [UIFont boldSystemFontOfSize:44];
        oilLabel.textColor = [UIColor whiteColor];
        [bgImageView addSubview:oilLabel];
        
    }
    return _tableHeaderView;
}

@end
